#include "../mqserver/exchange.hpp"
#include <gtest/gtest.h>

using namespace std;
using namespace ns_rabbitmq;
ns_rabbitmq::ExchangeManager::ExchangeManagerPtr emp;

class ExchangeTets : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        emp = std::make_shared<ns_rabbitmq::ExchangeManager>("./data/meta.db");
    }
    virtual void TearDown() override
    {
        //emp->clear();
        cout << "完成清理工作" << endl;
    }
};

TEST(exchange_test, test_insert)
{
    std::unordered_map<std::string, std::string> map{{"k1", "v1"}, {"k2", "v2"}};
    emp->DeclareExchange("machine1", ExchangeType::DIRECT, true, true, map);
    emp->DeclareExchange("machine2", ExchangeType::DIRECT, true, false, map);
    emp->DeclareExchange("machine3", ExchangeType::DIRECT, true, false, map);
    emp->DeclareExchange("machine4", ExchangeType::DIRECT, true, false, map);
    EXPECT_EQ(emp->Size(),4);
};

TEST(exchange_test, test_select)
{
    ExchangeMachine::MachinePtr exp = emp->SelectExchange("machine1");
    ASSERT_EQ(exp->_name, "machine1");
    ASSERT_EQ(exp->_exchange_type, ExchangeType::DIRECT);
    ASSERT_EQ(exp->_durable, true);
    ASSERT_EQ(exp->_auto_delete, true);
    ASSERT_EQ(exp->GetArgs(),"k2=v2&k1=v1");
}

TEST(exchange_test,test_remove)
{
    emp->DeleteExchange("machine1");
    ExchangeMachine::MachinePtr exp = emp->SelectExchange("machine1");
    ASSERT_EQ(exp.get(),nullptr);
    ASSERT_EQ(emp->IsExistsMachine("machine1"),false);
}

int main(int argc,char*argv[])
{
    testing::InitGoogleTest(&argc,argv);
    testing::AddGlobalTestEnvironment(new ExchangeTets());
    RUN_ALL_TESTS();
    return 0;
}